package com.karst.essentials.chapter09
{
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.core.animation.clip.AnimationClip3D;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.view.BasicView;
	
	internal class QuadTreeRenderingExample extends BasicView
	{
		
		private var rotX : Number = .1 ;
		private var rotY : Number = .1 ;
		private var easeOut : Number = .1 ;
		private var camYaw : Number = 270 ;
		private var camPitch : Number = 90 ;
		
		private var mill : DAE = null ;
		private var floor : Plane = null ;
		
		public function QuadTreeRenderingExample()
		{	
			createView();
		}
		
		private function createView():void
		{
			if( mill == null )
			{
				mill = new DAE( true, null, true ) ;
				mill.addEventListener( FileLoadEvent.LOAD_COMPLETE, loadedModelHandler ) ;
				DAE( mill ).load( "animatedMill.dae" ) ;
				DAE(mill).addFileSearchPath( "." ) ; // 材质搜索文件夹
			}
			
			if( floor == null )
			{
				var colorMat : ColorMaterial = new ColorMaterial( 0x006600 ) ;
				var wireMat : WireframeMaterial = new WireframeMaterial() ;
				var floorMat : CompositeMaterial = new CompositeMaterial() ;
				floorMat.addMaterial( colorMat ) ;
				floorMat.addMaterial( wireMat ) ;
				floorMat.doubleSided = true ;
				floor = new Plane( floorMat, 1000, 1000, 1, 1 ) ;
				floor.y = -410 ;
				floor.rotationX = 90 ;
			}
			scene.addChild( floor ) ;
			
			renderer = new QuadrantRenderEngine( QuadrantRenderEngine.CORRECT_Z_FILTER ) ;
		}
		
		private function loadedModelHandler( $event : FileLoadEvent ) : void
		{
			var animationRight : AnimationClip3D = new AnimationClip3D( "right", 0, 6 ) ;
			var animationLeft : AnimationClip3D = new AnimationClip3D( "left", 6, 12 ) ;
			DAE( mill ).animation.addClip( animationRight ) ;
			DAE( mill ).animation.addClip( animationLeft ) ;
			DAE( mill ).play( "right" ) ;
			scene.addChild( mill ) ;
			
			/** Enable for performance optimization
			 * 设置mill.testQuad = false;对子对象不会产生任何影响
			 */
			 mill.getChildByName("Blades",true).testQuad = false;
			 mill.getChildByName("Stand",true).testQuad = false;
			 mill.getChildByName("House",true).testQuad = false;
			
			startRendering();
		}
		
		override protected function onRenderTick( $event : Event = null ) : void
		{	
			super.onRenderTick( $event ) ;
			camPitch += (( mouseY - AsWingManager.getStage().stageHeight*.5) * rotX - camPitch + 90 ) * easeOut ;
			camYaw += (( mouseX - AsWingManager.getStage().stageWidth*.5) * rotY - camYaw + 270 ) * easeOut ;
			camera.orbit( camPitch, camYaw ) ;
		}
		
	}
}